package com.sise.stuteaintersys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sise.stuteaintersys.bean.entity.Admin;
import com.sise.stuteaintersys.bean.entity.Student;
import com.sise.stuteaintersys.bean.entity.Teacher;
import com.sise.stuteaintersys.mapper.AdminMapper;
import com.sise.stuteaintersys.mapper.StudentMapper;
import com.sise.stuteaintersys.mapper.TeacherMapper;
import com.sise.stuteaintersys.service.AdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @ClassName AdminUserServiceImpl
 * @Description TODO
 * @Date 2019/1/5 23:57
 * @Version 1.0
 **/
@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    TeacherMapper teacherMapper;

    @Autowired
    AdminMapper adminMapper;

    @Override
    public Boolean addStudent(Student student) {
        if (StringUtils.isEmpty(student)){
            return false;
        }
        student.setGmtCreate(new Date());
        int res = studentMapper.insert(student);
        return res == 1;
    }

    @Override
    public Boolean addStudentList(List<Student> students) {
        if (StringUtils.isEmpty(students)){
            return false;
        }
        //使用lambda表达式
        students.forEach(student -> student.setGmtCreate(new Date()));
        int res = students.stream().mapToInt(student -> studentMapper.insert(student)).sum();
        return res == students.size();
    }

    @Override
    public Boolean addTeacher(Teacher teacher) {
        if(StringUtils.isEmpty(teacher)){
            return false;
        }
        teacher.setGmtCreate(new Date());
        int res = teacherMapper.insert(teacher);
        return res == 1;
    }

    @Override
    public Boolean addTeacherList(List<Teacher> teachers) {
        if (StringUtils.isEmpty(teachers)){
            return false;
        }
        //使用lambda表达式
        teachers.forEach(teacher -> teacher.setGmtCreate(new Date()));
        int res = teachers.stream().mapToInt(teacher -> teacherMapper.insert(teacher)).sum();
        return res == teachers.size();
    }

    @Override
    public Boolean addAdmin(Admin admin) {
        if(StringUtils.isEmpty(admin)){
            return false;
        }
        admin.setGmtCreate(new Date());
        int res = adminMapper.insert(admin);
        return res == 1;
    }

    @Override
    public PageInfo<Student> findStudents(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Student> list = studentMapper.findAll();
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Student> findStudentByName(Integer pageNum, Integer pageSize, String name) {
        PageHelper.startPage(pageNum, pageSize);
        List<Student> list = studentMapper.findStudents(name);
        return new PageInfo<>(list);
    }

    @Override
    public int checkUser(String name, String status) {
        if(StringUtils.isEmpty(name) && StringUtils.isEmpty(status)){
            return 0;
        }
        switch (status){
            case "student":
                List<Student> studentList = studentMapper.checkStudent(name);
                return studentList.size();
            case "teacher":
                List<Teacher> teacherList = teacherMapper.checkTeacher(name);
                return teacherList.size();
            case "admin":
                List<Admin> adminList = adminMapper.checkAdmin(name);
                return adminList.size();
            default:
                return 0;
        }
    }

    @Override
    public PageInfo<Teacher> findTeachers(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Teacher> list = teacherMapper.findAll();
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Teacher> findTeacherByName(Integer pageNum, Integer pageSize, String name) {
        PageHelper.startPage(pageNum, pageSize);
        List<Teacher> list = teacherMapper.findTeachers(name);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Admin> findAdmins(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Admin> list = adminMapper.findAll();
        return new PageInfo<>(list);
    }

    @Override
    public int delStudent(Integer id) {
       return studentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int delTeacher(Integer id) {
        return teacherMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void delAdmin(Integer id) {
        adminMapper.deleteByPrimaryKey(id);
    }
}
